package com.bootdo.task.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.task.domain.TTaskDatilDO;
import com.bootdo.task.service.TTaskDatilService;
import com.bootdo.task.util.PackageTaskType;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 任务明细表
 * 
 * @author jsn
 * @email 1992lcg@163.com
 * @date 2018-04-26 13:25:18
 */
 
@Controller
@RequestMapping("/task/tTaskDatil")
public class TTaskDatilController {
	@Autowired
	private TTaskDatilService tTaskDatilService;
	@Autowired 
	private PackageTaskType packageTaskType;
	@GetMapping()
//	@RequiresPermissions("task:tTaskDatil:tTaskDatil")
	String TTaskDatil(@RequestParam(required = false) Long tdTaskId,Model model,HttpSession session){
		session.setAttribute("tdTaskId", tdTaskId);
	    return "task/tTaskDatil/tTaskDatil";
	}
	
	@ResponseBody
	@GetMapping("/list")
//	@RequiresPermissions("task:tTaskDatil:tTaskDatil")
	public PageUtils list(@RequestParam Map<String, Object> params,HttpSession session){
		params.put("tdTaskId", session.getAttribute("tdTaskId"))	;
		//查询列表数据
        Query query = new Query(params);
		List<TTaskDatilDO> tTaskDatilList = tTaskDatilService.list(query);
		for (TTaskDatilDO tTaskDatilDO : tTaskDatilList) {
			tTaskDatilDO = packageTaskType.packageTTaskDatilPojo(tTaskDatilDO);
		}
		int total = tTaskDatilService.count(query);
		PageUtils pageUtils = new PageUtils(tTaskDatilList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "task/tTaskDatil/add";
	}

	@GetMapping("/edit/{tdGid}")
//	@RequiresPermissions("task:tTaskDatil:edit")
	String edit(@PathVariable("tdGid") Long tdGid,Model model){
		TTaskDatilDO tTaskDatil = tTaskDatilService.get(tdGid);
		model.addAttribute("tTaskDatil", tTaskDatil);
	    return "task/tTaskDatil/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( TTaskDatilDO tTaskDatil){
		if(tTaskDatilService.save(tTaskDatil)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( TTaskDatilDO tTaskDatil){
		tTaskDatilService.update(tTaskDatil);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long tdGid){
		if(tTaskDatilService.remove(tdGid)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] tdGids){
		tTaskDatilService.batchRemove(tdGids);
		return R.ok();
	}
	
}
